package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.CapBacDao;
import com.tt.fashionWebSite.dao.PhanQuyenDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.CapBac;
import com.tt.fashionWebSite.domain.PhanQuyen;
import com.tt.fashionWebSite.domain.ThongTinThanhVien;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.service.HeaderService;
import com.tt.fashionWebSite.util.AutoMailTask;
import com.tt.fashionWebSite.util.CryptographyDataUtils;
import org.hibernate.*;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.stat.Statistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.NamingException;
import javax.naming.Reference;
import java.io.Serializable;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by 10520_000 on 11/6/14.
 */
@Service("headerService")
public class HeaderServiceImp extends GenericService implements HeaderService{
   @Autowired
   @Qualifier("thongTinThanhVien")
   private ThongTinThanhVienDao thongTinThanhVienDao;

   @Autowired
   @Qualifier("capBac")
   private CapBacDao capBacDao;

   @Autowired
   @Qualifier("phanQuyen")
   private PhanQuyenDao phanQuyenDao;

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public PhanQuyenDto insertInfoUser(PhanQuyenDto phanQuyenDto) {
      ThongTinThanhVien tinThanhVien = thongTinThanhVienDao.findById(phanQuyenDto.getThongTinThanhVien().getMaTV());
      if(tinThanhVien == null) {
         tinThanhVien = new ThongTinThanhVien();
         tinThanhVien.setMaTV(phanQuyenDto.getThongTinThanhVien().getMaTV());
         tinThanhVien.setGioiTinh(phanQuyenDto.getThongTinThanhVien().getGioiTinh());
         tinThanhVien.setHo(phanQuyenDto.getThongTinThanhVien().getHo());
         tinThanhVien.setTen(phanQuyenDto.getThongTinThanhVien().getTen());
         tinThanhVien.setNgaySinh(phanQuyenDto.getThongTinThanhVien().getNgaySinh());
         thongTinThanhVienDao.insert(tinThanhVien);
         // insert phan quyen
         PhanQuyen phanQuyen = new PhanQuyen();
         phanQuyen.setThongTinThanhVien(tinThanhVien);
         CapBac capBac = capBacDao.findById("CB3");
         phanQuyen.setCapBac(capBac);
         phanQuyen.setMatKhau(CryptographyDataUtils.encryptWithAES(phanQuyenDto.getMatKhau()));
         phanQuyenDao.insert(phanQuyen);

         // send mail
         AutoMailTask autoMail = new AutoMailTask();
         autoMail.sendMail("Fashion Shop", phanQuyenDto.getThongTinThanhVien().getMaTV(), "Đăng ký", "Chúc mừng bạn đã đăng ký thành công !");
         return phanQuyenDto;
      }
      else {
         return phanQuyenDto = null;
      }
   }



   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public LoginDto userLogin(LoginDto loginDto) {
      PhanQuyen phanQuyen = phanQuyenDao.findById(loginDto.getEmail());
      LoginDto result = null;
      if(phanQuyen != null && CryptographyDataUtils.decryptWithAES(phanQuyen.getMatKhau()).equals(loginDto.getPass())) {
         result = loginDto;
      }
      return result;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public LoginDto adminLogin(LoginDto loginDto) {
      PhanQuyen phanQuyen = phanQuyenDao.findById(loginDto.getEmail());
      LoginDto result = null;
      if(phanQuyen != null && CryptographyDataUtils.decryptWithAES(phanQuyen.getMatKhau()).equals(loginDto.getPass()) && phanQuyen.getCapBac().getMaCB().equals("CB1")) {
         result = loginDto;
      }
      return result;
   }
}
